package com.changpay.zywy.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ruffian.library.widget.RTextView;
import com.changpay.zywy.R;
import com.changpay.zywy.adapter.ProjectAdapter;
import com.changpay.zywy.bean.ProjectBean;
import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AndroidIntexh1 on 2018/12/26.
 */

public class SelectProjectDialog extends BaseCustomDialog {

    public OnDialogClickListener dialogListener;
    private RTextView btn_cancle, btn_sure;
    private RecyclerView mRecycleView;

    private static final String KEY_TYPE = "KEY_TYPE";
    private ProjectAdapter mAdapter;
    private List<ProjectBean> mDatas = new ArrayList<>();


    /**
     * 自定义builder来增加一些参数，记得要继承自BaseEasyDialog.Builder
     */
    public static class Builder extends BaseEasyDialog.Builder<SelectProjectDialog.Builder> {
        private Bundle bundle = new Bundle();
        private SelectProjectDialog.OnDialogClickListener listener;
        private List<ProjectBean> datas ;

        public Builder(@NonNull Context context) {
            super(context);
        }


        public SelectProjectDialog.Builder setOnDialogClickListener(SelectProjectDialog.OnDialogClickListener lis) {
            listener = lis;
            return this;
        }

        @NonNull
        @Override
        protected SelectProjectDialog createDialog() {
            SelectProjectDialog dialog = new SelectProjectDialog();
            dialog.setArguments(bundle);
            dialog.dialogListener = listener;
            dialog.mDatas = datas;
            return dialog;
        }

        public SelectProjectDialog.Builder setType( List<ProjectBean> datas) {
            this.datas = datas;
            return this;
        }


    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_select_project;
    }

    @Override
    protected void bindViews(View view) {
        mRecycleView = view.findViewById(R.id.project_recycle);
        btn_cancle = view.findViewById(R.id.tv_cancel);
        btn_sure = view.findViewById(R.id.tv_submit);
        initRecycleView();

    }

    private void setListener() {
        btn_cancle.setOnClickListener(v -> {
            dismiss();
        });
        btn_sure.setOnClickListener(v -> {
            if (projectID!=-1) dialogListener.onClick(v,projectID);
            dismiss();
        });
    }

    private void setData() {
    }

    @Override
    protected void setViews() {
        setData();
        setListener();
        setBackground();
        setLayout();

    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
//        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_shop_jion_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();

        // 强制宽高
        int padding = getResources().getDimensionPixelOffset(R.dimen.dialog_count_padding);
        lp.width = getScreenWidth(getActivity()) - (padding * 2);
//        lp.height = getResources().getDimensionPixelOffset(R.dimen.dialog_height);

        lp.gravity = Gravity.CENTER; // 设置展示的位置
//        lp.y = getResources().getDimensionPixelOffset(R.dimen.join_shop_dialog_margin);
        window.setAttributes(lp);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 监听输入框内容
     */
    public interface OnDialogClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, int type);
    }


    private int projectID = -1 ;
    private BaseQuickAdapter.OnItemClickListener onItemClickListener = (adapter, view, position) -> {
        for (ProjectBean projectBean : mDatas)
        {
            projectBean.setSelected(false);
        }
        mDatas.get(position).setSelected(true);
        projectID =  mDatas.get(position).getGc_id();
        mAdapter.notifyDataSetChanged();
    };



    private void initRecycleView() {
        //     设置布局控制器
        //  reverseLayout 是否实现反转
//        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecycleView.setLayoutManager(layoutManager);
        mRecycleView.setNestedScrollingEnabled(false);
        mAdapter = new ProjectAdapter(getContext(),mDatas);
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setOnItemClickListener(onItemClickListener);

    }


}
